package com.ruoyi.constant;


import cn.hutool.core.date.DateUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 *  日期相关的工具类
 */
public class K_DateUtil {

    /**
     * 返回两个时间内的时间数组 单位为分钟
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param formatType 要格式化的时间格式
     * @param subNum 间隔多久取一次 //单位为分钟
     * @return
     * 例：入参->2023-04-01 12:20,  2023-04-01 12:35, "yyyy-MM-dd HH:mm", 5
     * 返回： [2023-04-01 12:20,2023-04-01 12:25,2023-04-01 12:30,2023-04-01 12:35]
     */
    public static List<String> getCategoryByMinutes(String startTime, String endTime,String formatType, int subNum){
        List<String> data = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatType);
        long sTime = 0;
        long eTime = 0;
        try {
            sTime = simpleDateFormat.parse(startTime).getTime();
            eTime = simpleDateFormat.parse(endTime).getTime();
        }catch (Exception e){
            eTime = new Date().getTime();
            sTime = new Date().getTime()-30*60*1000;
        }
        long time = eTime-sTime;
        long subTime = subNum*60*1000;
        for(int i=0;i<=time/subTime;i++){
            Date date = new Date(sTime);
            String dateString = simpleDateFormat.format(date);
            data.add(dateString);
            sTime = sTime+subTime;
        }
        return data;
    }

    /**
     * 返回两个时间内的时间数组 单位为 天
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param formatType 要格式化的时间格式
     * @return
     * 例：入参->2023-04-01,  2023-04-05, "yyyy-MM-dd
     * 返回： [2023-04-01,2023-04-02,2023-04-03,2023-04-04,2023-04-05]
     */
    public static List<String> getCategoryByDay(String startTime,String endTime,String formatType){
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        try {
            Calendar calStart = Calendar.getInstance();
            calStart.setTime(sdf.parse(startTime));
            Calendar calEnd = Calendar.getInstance();
            calEnd.setTime(sdf.parse(endTime));
            long millisecondsEnd = calEnd.getTimeInMillis();
            long millisecondsBegin = calStart.getTimeInMillis();
            long diff = millisecondsEnd - millisecondsBegin;//相差的毫秒值
            long diffDays = diff / (24 * 60 * 60 * 1000); //相差的天数
            List<String> category = new ArrayList<>();
            category.add(endTime);
            for(int i=0;i<diffDays;i++){
                calEnd.add(Calendar.DATE,-1);
                category.add(sdf.format(calEnd.getTime()));
            }
            Collections.sort(category);
            return category;
        }catch (Exception e){
            return null;
        }
    }


    /**
     * 返回两个时间相差的天数
     * @param startTime
     * @param endTime
     * @param formatType
     * @return
     */
    public static long getSubDay(String startTime,String endTime,String formatType){
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        try {
            Calendar calStart = Calendar.getInstance();
            calStart.setTime(sdf.parse(startTime));
            Calendar calEnd = Calendar.getInstance();
            calEnd.setTime(sdf.parse(endTime));
            long millisecondsEnd = calEnd.getTimeInMillis();
            long millisecondsBegin = calStart.getTimeInMillis();
            long diff = millisecondsEnd - millisecondsBegin;//相差的毫秒值
            long diffDays = diff / (24 * 60 * 60 * 1000); //相差的天数
            return diffDays;
        }catch (Exception e){
            return 0;
        }
    }


    /**
     * 获取系统当前时间戳
     * @return 1566889186583
     */
    public static String getSystemTime() {
        String current = String.valueOf(System.currentTimeMillis());
        return current;
    }

    /**
     * 获取当前日期 默认为:yy-MM-dd
     * @return 2023-04-01
     */
    public static String getSystemDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String getSystemDate(String formatType) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        return sdf.format(date);
    }

    /**
     * 获取当前日期 integer类型
     * @return
     */
    public static Integer getSysDate(){
        String formatDate = DateUtil.format(new Date(), "yyyyMMdd");
        return Integer.parseInt(formatDate);
    }

    /**
     * 获取当前时间 integer类型
     * @return
     */
    public static Integer getSysTime(){
        String formatTime = DateUtil.format(new Date(), "HHmmss");
        return Integer.parseInt(formatTime);
    }


}
